Page({
  data: {
    currentDate: new Date().getTime(),
    houseLayerVisible: false,
    repairLayerVisible: false,
    dateLayerVisible: false,
    houseList: [],
    repairItem: [],

    houseListItem: '',
    params: {
      repairItemId: '', //维修项目id
      houseId: '', //已通过审核的房屋id
      appointment: '',
    },
    mobile: '',
    description: '', //问题描述
    repairItemActve: '',
    attachment: [],
  },
  selectDate(ev) {
    this.setData({
      dateLayerVisible: false,
      'params.appointment': wx.utils.formatDate(ev.detail),
    })
  },
  openHouseLayer() {
    this.setData({ houseLayerVisible: true })
  },
  closeHouseLayer() {
    this.setData({ houseLayerVisible: false })
  },
  openRepairLayer() {
    this.setData({ repairLayerVisible: true })
  },
  closeRepairLayer() {
    this.setData({
      repairLayerVisible: false,
    })
  },

  openDateLayer() {
    this.setData({ dateLayerVisible: true })
  },
  closeDateLayer() {
    this.setData({ dateLayerVisible: false })
  },
  goList() {
    wx.reLaunch({
      url: '/repair_pkg/pages/list/index',
    })
  },
  // 上传问题附件
  afterRead(ev) {
    // 临时文件
    const { file } = ev.detail
    // 调用接口上传至服务器
    wx.uploadFile({
      url: wx.http.baseURL + '/upload',
      filePath: file.url,
      name: 'file',
      header: {
        Authorization: getApp().token,
      },
      success: (res) => {
        // 转换 json 数据
        const data = JSON.parse(res.data)
        // 上传完成需要更新
        const { attachment = [] } = this.data
        attachment.push({ ...data.data })
        // 渲染数据
        this.setData({ attachment })
      },
    })
  },
  async getMyRoomList() {
    const { code, data } = await wx.http.get('/room')
    //
    if (code !== 10000) return wx.utils.toast('获取房屋列表失败!')
    const houseList = data.map(({ point, building, room, id }) => {
      return {
        name: `${point} ${building} ${room}室`,
        id,
      }
    })
    this.setData({ houseList })
  },
  async getRepair() {
    const { code, data: repairItem } = await wx.http.get('/repairItem')
    if (code !== 10000) return wx.utils.toast('获取维修项目失败!')
    this.setData({ repairItem })
  },
  // 解构id
  onLoad({ id }) {
   if(id){
     wx.setNavigationBarTitle({
       title: '修改报修信息',
     })
    //  通过id获取详情
    this.getRepairDetail(id)
   }
  },
  // 获取详情
 async getRepairDetail(id){
    if(!id) return
    // 请求数据
    const { code, data: repairDetail } = await wx.http.get('/repair/' + id)
    // 校验调用结果
    if(code!== 10000) return wx.utils.toast('获取报修详情失败')
    this.setData({
      ...repairDetail
    })

  },

  onShow() {
    this.getMyRoomList()
    this.getRepair()
  },
  selectHouseItem({ detail }) {
    const { name: houseListItem, id } = detail
    this.setData({ houseListItem, 'params.houseId': id })
  },
  selectRepairItem({ detail: { name, id } }) {
    this.setData({ 'params.repairItemId': id, repairItemActve: name })
  },
  // 删除图片
  onDelete(ev) {
    // console.log(ev);
    const { attachment } = this.data
    attachment.splice(ev.detail.index, 1)
    this.setData({
      attachment,
    })
  },
  //进行提交校验
  async submit() {
    if (!wx.verify.verifyHouse(this.data.params.houseId)) return
    if (!wx.verify.verifyRepair(this.data.params.repairItemId)) return
    if (!wx.verify.verifyMobile(this.data.mobile)) return
    if (!wx.verify.verifyDate(this.data.params.appointment)) return
    if (!wx.verify.verifyDescription(this.data.params.description)) return
    // 调用接口完成跳转

    const { repairItemId, houseId, appointment } = this.data.params
    const { mobile, description, attachment,id } = this.data
    const { code } = await wx.http.post('/repair', {
      repairItemId,
      houseId,
      appointment,
      mobile,
      description,
      attachment,
      id
    })
    if (code !== 10000) return wx.utils.toast('提交保修失败')
    // jinxing
  },
})
